﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using SmartSpace.Core;

namespace SsTool.Base.Windows
{
    /// <summary>
    /// Interaction logic for ModuleWindow.xaml
    /// </summary>
    public partial class ModuleWindow : Window
    {
        public Module Module { get; private set; }

        public ModuleWindow()
        {
            InitializeComponent();

            Module = new Module();
            Module.Id = Guid.NewGuid();

            this.DataContext = Module;
        }

        public ModuleWindow(Module module)
        {
            InitializeComponent();

            Module = module;

            this.DataContext = Module;
        }

        private void _ctlBtnCreate_Click(object sender, RoutedEventArgs e)
        {
            ViewWindow window = new ViewWindow();
            if (window.ShowDialog() == true)
            {
                if (Module.Views == null)
                {
                    Module.Views = new List<ModuleView>();
                }

                Module.Views.Add(window.View);

                this.DataContext = null;
                this.DataContext = Module;
            }
        }

        private void _ctlBtnUpdate_Click(object sender, RoutedEventArgs e)
        {
            ModuleView selectedView = this._ctlGridView.SelectedItem as ModuleView;
            if (selectedView != null)
            {
                ViewWindow window = new ViewWindow(selectedView);
                if (window.ShowDialog() == true)
                {
                    this.DataContext = null;
                    this.DataContext = Module;
                }
            }
        }

        private void _ctlSubmit_Click(object sender, RoutedEventArgs e)
        {
            ICoreService service = App.Get<ICoreService>();
            if (service.SaveModule(Module))
            {
                DialogResult = true;
            }
        }
    }
}
